//https://leetcode.cn/problems/find-pivot-index/
public class Test {
    public static void main(String[] args) {
        //
    }
}


class Solution {
    public int pivotIndex(int[] nums) {
        int n = nums.length;
        //预处理两个数组
        int[] f = new int[n];
        int[] g = new int[n];

        f[0] = 0;//f[i]：表示i下标左边的所有元素的和
        for (int i = 1; i < n; i++) {
            f[i] = f[i - 1] + nums[i - 1];
        }

        g[n - 1] = 0;//g[i]：表示i下标右边的所有元素的和
        for (int i = n - 2; i >= 0; i--) {
            g[i] = g[i + 1] + nums[i + 1];
        }

        for (int i = 0; i < n; i++) {
            if(f[i] == g[i]){
                return i;
            }
        }
        return -1;
    }
}